//
//  YDYFanJuHeaderReusableView.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/9/28.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYFanJuHeaderReusableView.h"
#import <AFNetworking.h>
#import "YDYOldBanner.h"
#import "YDYFanju.h"
#import "NSObject+Property.h"
#import "YDYFanJuNewsView.h"
#import <UIImageView+WebCache.h>

#import "YDYCyclePhotoView.h"
@interface YDYFanJuHeaderReusableView()<UIScrollViewDelegate>
//顶部图片轮播器
@property (weak, nonatomic) IBOutlet UIScrollView *bannerScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *bannerPageControl;

/** 网络请求回来的数据 */
@property (nonatomic, strong) NSArray *dataArray;
/** 番剧图片轮播器数组 */
@property (nonatomic, strong) NSArray *bannerArray;
/** 图片轮播器下面的4个正方形数据 */
@property (nonatomic, strong) NSArray *newsArray;
/** 计时器 */
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation YDYFanJuHeaderReusableView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupScrollView];
    [self getADDatafromUrl];
    [self getDatafromUrl];
    
}
-(void)setNewsArray:(NSArray *)newsArray{
    _newsArray = newsArray;
    
    for (int i = 0; i < newsArray.count; i++) {
        UIView *newView = [self viewWithTag:i+1];
        newView.layer.cornerRadius = 10;
        
        
        YDYFanJuNewsView *fanjuNewView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YDYFanJuNewsView class]) owner:nil options:nil]lastObject];
        fanjuNewView.fanju = newsArray[i];
        fanjuNewView.frame = newView.bounds;
        [newView addSubview:fanjuNewView];
   
    }
    
}


-(void)setBannerArray:(NSArray *)bannerArray{
    _bannerArray = bannerArray;
    self.bannerScrollView.contentSize = CGSizeMake(YDYScreenSize.width * bannerArray.count, 0);
    //设置图片
    for (int i = 0; i < bannerArray.count; i++) {
        YDYOldBanner *banner = bannerArray[i];
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(i * YDYScreenSize.width, 0, YDYScreenSize.width, self.bannerScrollView.frame.size.height)];

        [imgview sd_setImageWithURL:[NSURL URLWithString:banner.img]];
        [self.bannerScrollView addSubview:imgview];
    }
    //启动计时器
    [self timer];
    
    


    
}

-(void)setupScrollView{
    self.bannerScrollView.pagingEnabled = YES;
    self.bannerScrollView.showsVerticalScrollIndicator = NO;
    self.bannerScrollView.showsHorizontalScrollIndicator = NO;
    self.bannerScrollView.delegate = self;
    self.bannerScrollView.bounces = NO;
    
    self.bannerPageControl.currentPage = 0;
   
}
//-(void)beginTimer{
////    self.timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
////    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//   
//    
////    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
//}


-(void)nextPage{

    if (self.bannerScrollView.contentOffset.x == YDYScreenSize.width * (self.bannerArray.count - 1)) {
        [self.bannerScrollView setContentOffset:CGPointMake(0, self.bannerScrollView.contentOffset.y) animated:YES];
       
        self.bannerPageControl.currentPage = 0;
    }else{
        [self.bannerScrollView setContentOffset:CGPointMake(self.bannerScrollView.contentOffset.x + YDYScreenSize.width, self.bannerScrollView.contentOffset.y) animated:YES];
        self.bannerPageControl.currentPage += 1;
        
    }
}
#pragma mark -
#pragma mark -scrollview delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_timer invalidate];
    _timer = nil;
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int supposePage = round(self.bannerScrollView.contentOffset.x / YDYScreenSize.width);
    self.bannerScrollView.contentOffset = CGPointMake(YDYScreenSize.width * (supposePage), self.bannerScrollView.contentOffset.y);
    self.bannerPageControl.currentPage  = supposePage;
    [self timer];
}




#pragma mark -
#pragma mark -网络请求
-(void)getDatafromUrl{
    
//    @"http://api.bilibili.com/online_list?_device=android&_hwid=130a7709aeac1793&appkey=c1b107428d337928&platform=android&typeid=13&sign=cb5cf6d54ed92fc25c4a8b4292a46692" 
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://api.bilibili.com/online_list";
    NSDictionary *parameters = @{@"_device=":@"android",
                                 @"_hwid=":@"130a7709aeac1793",
                                 @"appkey=":@"c1b107428d337928",
                                 @"platform=":@"android",
                                 @"typeid=":@"13",
                                 @"sign=":@"cb5cf6d54ed92fc25c4a8b4292a46692"};

    [manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *listarray = responseObject[@"list"];
        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:30];
        for (NSDictionary *dict in listarray) {
            YDYFanju *fanju = [YDYFanju ydyFanjuWithDict:dict];
            [temp addObject:fanju];
        }
        self.dataArray = temp;
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YDYLog(@"番剧网络请求错误=%@",error);
    }];
}
-(void)getADDatafromUrl{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://app.bilibili.com/api/region2/13.json?_device=android&_hwid=130a7709aeac1793&appkey=c1b107428d337928&build=402003&platform=android&ts=1449758668000&sign=9f36da5e90b8345e376a71466a078696" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        NSDictionary *resultArray = responseObject[@"result"];
        
        //1.图片轮播器原始数据
        NSArray *bannersArray = resultArray[@"banners"];
        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:4];
        for (NSDictionary *dict  in bannersArray) {
            YDYOldBanner *banner = [YDYOldBanner ydybannerWithDict:dict];
            [temp addObject:banner];
        }
        self.bannerArray = temp;
        self.bannerPageControl.numberOfPages = self.bannerArray.count;
        //2.他们都在看什么的原始数据（就是图片轮播器下面的4个框框的数据）
        NSArray *newsArray = resultArray[@"news"];
        temp = [NSMutableArray arrayWithCapacity:4];
        for (NSDictionary *dict in newsArray) {
            YDYFanju *fanju = [YDYFanju ydyFanjuWithDict:dict];
            [temp addObject:fanju];
        }
        self.newsArray = temp;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YDYLog(@"%@",error);
    }];
    
}


#pragma mark -
#pragma mark -lazy load
-(NSTimer *)timer{
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
        
    }
    return _timer;
}

@end
